<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<ny_lizhong@126.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\Category as CategoryModel;

/**
 * 分类控制器
 *
 * @author 李忠<ny_lizhong@126.com>
 * @createAt 2018-09-19 17:16:29
 *
 */
class Category extends Admin {

    /**
     * 列表页面
     */
    public function index($status = null, $type = null, $title = null) {
        cookie('__forward__', $this->request->url());

        // 数据列表
        $data_list = CategoryModel::lists($status, $type, $title);
        $this->assign('data_list', $data_list);
        $this->assign('pager', $data_list->render());

        $this->assign('category_type', config('category_type'));
        $this->assign('data_status', config('data_status'));

        $this->assign('status', $status);
        $this->assign('type', $type);
        $this->assign('title', $title);
        $this->setMeta('分类管理');
        return $this->fetch();
    }

    // 新增分类
    public function add() {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Category');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = CategoryModel::create($data)) {
                $this->log($data['id'], '新增分类成功');
                $this->success('新增分类成功', cookie('__forward__'));
            } else {
                $this->error('新增分类失败');
            }
        } else {
            $this->assign('info', null);
            $this->setMeta('新增分类');
            return $this->fetch();
        }
    }

    // 更新分类
    public function edit($id = null) {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Category');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = CategoryModel::update($data)) {

                $this->log($data['id'], '更新分类成功');
                $this->success('更新分类成功', cookie('__forward__'));
            } else {
                $this->error('更新分类失败');
            }

        } else {
            $info = CategoryModel::get($id);

            $this->assign('info', $info);
            $this->setMeta('更新分类');
            return $this->fetch('add');
        }
    }

    /**
     * 禁用
     * @param int $id
     */
    public function forbid($id) {
        if (CategoryModel::state($id, -1)) {
            $this->log($id, '禁用分类成功');
            $this->success('禁用分类成功');
        } else {
            $this->error('禁用分类失败');
        }
    }

    /**
     * 恢复
     * @param int $id
     */
    public function resume($id) {
        if (CategoryModel::state($id, -1)) {
            $this->log($id, '启用分类成功');
            $this->success('启用分类成功');
        } else {
            $this->error('启用分类失败');
        }
    }

    /**
     * 删除
     * @param int $id
     */
    public function delete($id) {
        if (CategoryModel::state($id, -1)) {
            $this->log($id, '删除分类成功');
            $this->success('删除分类成功');
        } else {
            $this->error('删除分类失败');
        }
    }

}